from tortoise.models import Model
from tortoise import fields, connections

class MixinTimeFiled:
    # 添加数据时间
    created = fields.DatetimeField(null=True, auto_now_add=True)
    # 修改数据时间
    updated = fields.DatetimeField(null=True, auto_now=True)

# https://tortoise-orm.readthedocs.io/en/latest/
class Moji(Model, MixinTimeFiled):
    cityId = fields.IntField(pk=True)
    name = fields.CharField(50)

    def __str__(self):
        return f"{self.cityId}: {self.name}"

    def __dict__(self):
        return {
            'cityId': self.cityId,
            'name': self.name
        }

    @classmethod
    async def query(cls, query):
        db = connections.get("default")
        result = await db.execute_query_dict(query)
        return result